<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['diy']=$this->plugin_setting('diy');
if($_RQ['op']=='main'){
	exi($return);
}
$return['ispay']=array('0'=>'未支付','1'=>'已支付');
/*******op*******/
set_html('title','配送订单');
//paisong_type,指定配送员的类型（0所有，1平台，2商户自己的员工）
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>0);
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['status']=$_RQ['status'];
	}else{
		$return['status']=0;
	}
	$merch=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$merchid));
	$cateid2=str_array($merch['cateid2']);
	if($merch['cateid2']){
		$params['cate_id']=$cateid2?:-1;
		$pcatetitle='';
		foreach($cateid2 as $cvid){
			$category=pdo_get('jy_merch_category',array('uniacid'=>$_SESSION['uniacid'],'id'=>$cvid),array('pcate','catename'));
			$pcate=$category['pcate'];
			$nowtitle .=pdo_getcolumn('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'id'=>$pcate),'title');
			$pcatetitle .=$nowtitle.',';
		}
		$tips='您当前可抢单《'.$pcatetitle.'》商品类型的订单！';
	}else{
		$tips='您未设置商户分类，当前可抢单《所有商品类型》的订单！';
	}
	$return['tips']=$tips;
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$order_list=pdo_getslice('jy_liangoods_sendlog',$params,array($pindex,$psize),$total,array(),'','createtime desc');
	if($order_list){
		foreach($order_list as &$item){
			$item['createtime']=date('Y-m-d H:i:s', $item['createtime']);
			$order=pdo_get('jy_weishop_order',array('id'=>$item['order_id'],'uniacid'=>$_SESSION['uniacid']),array('is_peisong','paytype','ordersn','ispay','addressid'));
            if($order['is_peisong']==1){
                $item['sendtype']=4;
            }
            $item['sendtypetitle']=$this->send_types[$item['sendtype']];
			$item['paytype']=$order['paytype'];
			$item['ispay']=$order['ispay'];
			//用户地址
			$address['pcd']=json_decode($item['pcd'],true);
			$address['name']=$item['name'];
			$address['mobile']=$item['mobile'];
			$item['order_out']=$address;
			if($item['goods']){
				$item['goods']=json_decode($item['goods'],true);
			}
			$selfinfo=pdo_get('jy_weishop_order_self',array('orderid'=>$item['order_id']));
			$selfinfo['selftime']=$selfinfo['selftime']?date('Y-m-d H:i:s',$selfinfo['selftime']):'尽快配送';
			$selfinfo['selftime']=$selfinfo['out_time']?:$selfinfo['selftime'];
			$item['selfinfo']=$selfinfo;
			$item['total']=pdo_getsum('jy_weishop_order_goods',array('orderid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'total');
		}
	}
	$return['list']=$order_list;
	if($this->session('isopenvoice')){
		$return['isopenvoice']=$this->session('isopenvoice')!=2?1:0;	
	}else{
		$return['isopenvoice']=1;
	}
	exi($return);
}

//订单详情
if($_RQ['op']=='detail'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择','error');
	}
	$order=pdo_get('jy_liangoods_sendlog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	//用户信息
	$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['mid']));
	$user=pdo_get('jy_weishop_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['mid']));
	//用户地址
	$address['pcd']=json_decode($order['pcd'],true);
	$address['name']=$order['name'];
	$address['mobile']=$order['mobile'];
	$order['order_out']=$address;
	if($order['goods']){
		$order['goods']=json_decode($order['goods'],true);
	}
	$order['paytime']=$order['paytime']?date('Y-m-d H:i:s', $order['paytime']):'';
	//自定义字段数据
	$return['order']=$order;
	$return['user']=$user;
	$return['member']=$member;
	exi($return);
}
//员工确认订单
if($_RQ['op']=='getorder'){
	$log_id=intval($_RQ['id']);
	if(!$log_id){
		exi('订单不存在，请重新选择订单','error');
	}
	$params=array('log_id'=>$log_id,'merchid'=>$merchid);
	$this->model('merch')->order_get($params);
	exi('接收订单任务成功!','','/merch.worker_order/');
}
//员工取消订单
if($_RQ['op']=='unreceive'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_update('jy_liangoods_sendlog',array('status'=>0),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('取消订单成功');
}